package com.battcn.platform.service.sys.impl;

import com.battcn.framework.common.model.TreeNode;
import com.battcn.framework.mybatis.page.DataGrid;
import com.battcn.platform.mapper.sys.OfficeMapper;
import com.battcn.platform.pojo.dto.sys.ManagerDto;
import com.battcn.platform.pojo.po.sys.Office;
import com.battcn.platform.service.sys.OfficeService;
import com.battcn.platform.util.SessionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static java.util.stream.Collectors.toList;

/**
 * @author Levin
 */
@Service
public class OfficeServiceImpl implements OfficeService {


    private final OfficeMapper officeMapper;


    @Autowired
    public OfficeServiceImpl(OfficeMapper officeMapper) {
        this.officeMapper = officeMapper;


    }

    private List<TreeNode> createTree(List<Office> offices, Integer pid) {
        return offices.stream().filter(m -> m.getPaterId() != null && Objects.equals(pid, m.getPaterId()))
                .map(m -> new TreeNode(m.getId(), m.getName(), m.getIcon())).collect(toList());
    }

    @Override
    public List<TreeNode> listTree() {
        List<TreeNode> trees = Lists.newArrayList();
        List<Office> offices = officeMapper.listOffice();
        offices.stream().filter(m -> m.getPaterId() == null).forEach(m -> {
            List<TreeNode> tree = createTree(offices, m.getId());
            if (CollectionUtils.isNotEmpty(tree)) {
                trees.add(new TreeNode(m.getId(), m.getName(), m.getIcon(), tree));
            }
        });
        return trees;
    }


    @Override
    public PageInfo<Office> listOfficeForDataGrid(DataGrid grid) {
        return PageHelper.startPage(grid.getPageNum(), grid.getPageSize()).doSelectPageInfo(this.officeMapper::listOffice);
    }

    @Override
    public List<Office> listOffice() {
        List<Office> offices = this.officeMapper.listOffice();
        offices.forEach(m -> m.setName(m.getPaterId() == null ? "－－" + m.getName() : "－－－－" + m.getName()));
        return offices;
    }

    @Override
    public void saveOrUpdate(Office office) {

        if (office.getId() != null) {
            ManagerDto user = SessionUtil.getSession();
            office.setUpdateBy(user.getId());   //修改人
            office.setUpdateDate(new Date());   //修改时间
            officeMapper.updateByPrimaryKeySelective(office);
        } else {
            office.setCreateDate(new Date());   //创建时间
            ManagerDto user = SessionUtil.getSession();
            office.setCreateBy(user.getId());   //创建人
            office.setUpdateBy(user.getId());   //修改人
            office.setUpdateDate(new Date());   //修改时间
            officeMapper.insertSelective(office);
        }
        this.officeMapper.refreshOfficeTreeNodes();
    }
    @Override
    public Optional<Office> selectById(Object key) {
        return Optional.ofNullable(officeMapper.selectByPrimaryKey(key));
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Object key) {
        return officeMapper.deleteById((Integer) key) > 0;
    }

}
